package com.moke.log
{
	import com.moke.log.appender.TraceAppender;
	import com.moke.format.DateFormat;
	
	import flash.utils.getQualifiedClassName;
	
	/**
	 * 日志打印系统类
	 * @author fraser 
	 * 
	 * 加入时间戳
	 * 
	 */	
	public class Logger
	{		
		public static var LOGGER_LEVEL:int = LogLevel.DEBUG ;		
		
		private static var _appenders:Vector.<IAppender> = Vector.<IAppender>([new TraceAppender()]);
				
		public static function addAppender(appender:IAppender):void
		{
			_appenders.push(appender);
		}
		
		public static function debug(msg:String,target:Object = null):void
		{
			log(LogLevel.DEBUG, msg,target);
		}
		
		public static function info(msg:String,target:Object = null):void
		{
			log(LogLevel.INFO, msg,target);
		}
		
		public static function warn(msg:String,target:Object = null):void
		{
			log(LogLevel.WARN, msg,target);
		}
		
		public static function error(msg:String,target:Object = null):void
		{
			log(LogLevel.ERROR, msg,target);
		}
		
		public static function fatal(msg:String,target:Object = null):void
		{
			log(LogLevel.FATAL, msg,target);
		}	
		
		private static function log(level:int, msg:String ,target:Object):void
		{			
			if(level < LOGGER_LEVEL)
			{
				return;
			}
			var clsName:String = "-" ;
			if(target != null)
			{
				clsName = getQualifiedClassName(target) ;
			}
			
			var appenderMsg:String = preprocessMsg(level, msg,clsName);
			for each(var appender:IAppender in _appenders)
			{
				appender.append(level,appenderMsg);
			}
		}
		
		
		private static function preprocessMsg(level:int, msg:String ,name:String):String
		{
			var result:String =  "(" + name + ") time:[" + new DateFormat(":").format(new Date()) + "] " + msg;
			switch(level)
			{
				case LogLevel.DEBUG:
					result = "Debug: " + result;
					break;
				case LogLevel.INFO:
					result = "Info: " + result;
					break;
				case LogLevel.WARN:
					result = "Warn: " + result;
					break;
				case LogLevel.ERROR:
					result = "Error: " + result;
					break;
				case LogLevel.FATAL:
					result = "Fatal: " + result;
					break;
			}
			return result;
		}
	}
}
class Blocker{}